<template>
  <div class="word-document-page">
    <nav-bar :title="$route.meta.title" />
    <div class="container">
      <div class="content">
        <div class="message-card">
          <h2 class="message-title">{{ title }}</h2>
          <p class="message-text text-truncate" v-html="formattedContent" />
        </div>
        <!-- 其他消息卡片 -->
      </div>
    </div>
  </div>
</template>

<script>
import NavBar from '@/components/NavBar'
import { getWords } from '@/api/user'

export default {
  components: {
    NavBar
  },
  data() {
    return {
      title: '',
      content: '',
      id: '',
      wordList: []
    }
  },
  computed: {
    formattedContent() {
      return this.content
        .replace(/\r\n/g, '<br>')
        .replace(/^(.+?)(\r\n|$)/g, (match, p1) => `<span ">${p1}</span><br>`)
    }
  },
  created() {
    this.id = this.$route.params.id
  },
  mounted() {
    this.getWordsData()
  },
  methods: {
    async getWordsData() {
      const res = await getWords()
      if (res && res.map && res.map.words) {
        this.wordList = res.map.words
        const word = res.map.words.filter((item) => {
          return item.id === this.id
        })
        if (!word.length) return
        console.log('this.id:', this.id, word)
        this.content = word[0].wordContent
        this.title = word[0].wordName
      }
    }
  }
}
</script>

<style scoped>
.word-document-page {
  background: #f5f5f5;
  min-height: 100vh;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.document-title {
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 20px;
  text-align: center;
}

.document-content {
  font-size: 16px;
  line-height: 1.6;
  text-align: justify;
}

.document-content img {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 20px auto;
}

.document-content table {
  width: 100%;
  border-collapse: collapse;
  margin: 20px 0;
}

.document-content th,
.document-content td {
  padding: 10px;
  border: 1px solid #ddd;
}

.document-content th {
  background-color: #f5f5f5;
  font-weight: bold;
}

.container {
  padding: 1rem;
  height: calc(100% - 60px);
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}
.content {
  height: 100%;
}
.header-left {
  display: flex;
  align-items: center;
}

.back-button,
.close-button {
  color: #a1a1aa;
}

.title {
  margin-left: 0.5rem;
  font-size: 1.125rem;
  font-weight: bold;
  color: #18181b;
}

.message-card {
  background-color: #ffffff;
  padding: 1rem;
  border-radius: 0.5rem;
  box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06);
}

.message-title {
  color: #18181b;
  font-weight: bold;
}

.message-text {
  color: #71717a;
  margin-top: 0.5rem;
}
</style>
